from django.shortcuts import render
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.http import JsonResponse
from django.urls import reverse
from django.forms.models import model_to_dict

from .forms import SpiderForm, TaskForm
from .models import Spider
from .models import Task

from lib.scrapyd_api import ScrapydClient

# Create your views here.
import requests
import time


def home(request):
    return render(request, 'spider/home.html')


def list_spiders(request):
    page = request.GET.get("page", "1")
    size = request.GET.get("size", "20")
    try:
        page = int(page)
    except:
        page = 1
    try:
        size = int(size)
    except:
        size = 20
    start = (page - 1) * size
    end = start + size
    spiders = Spider.objects.order_by("-update_time")[start:end]
    page_count = Spider.objects.count() // size + 1
    cur_page = page
    return render(request, 'spider/crawler.html', 
                  {"spiders":spiders, 
                   "cur_page":cur_page,
                   "size":size,
                   "pages":range(page_count)})


def add_spider(request):
    """
    增加爬虫
    """
    if request.method == "POST":
        spider_form = SpiderForm(request.POST)
        if spider_form.is_valid():
            spider_form.save(commit=True)
    form = SpiderForm()
    return render(request, 'spider/spider_add.html', {"form":form})


def del_spider(request):
    """
    删除爬虫
    """
    sid = request.GET.get("sid")
    try:
        sid = int(sid)
        Spider.objects.filter(id=sid).delete()
    except:
        pass
    return HttpResponseRedirect(reverse("spiders"))


def update_spider(request, sid):
    """
    更新爬虫
    """
    if request.method == "GET":
        spider= Spider.objects.get(id=sid)
        if not spider:
            raise HttpResponse("sid=%s 不存在" % sid)
        else:
            form = SpiderForm(initial= model_to_dict(spider))
            return render(request, 'spider/spider_add.html', {"form":form})

    elif request.method == "POST":
        form = SpiderForm(request.POST)
        if form.is_valid():
            spider = Spider.objects.get(id=sid)
            form_data = form.cleaned_data
            fields = form_data.keys()
            for field in fields:
                setattr(spider,field,form_data[field])
            spider.save()
        return HttpResponseRedirect(reverse("spiders"))
        

def spider_detail(request, sid):
    spider = Spider.objects.get(id=sid)
    keys = Spider.get_sorted_keys()
    values = [getattr(spider, k) for k in keys]
    spider_id = spider.id
    
    tasks = spider.task_set.all().order_by("-task_id")
    return render(request, "spider/spider_detail.html", 
            {"kv": zip(keys, values), "spider_id":spider_id, "tasks":tasks, "spider":spider})


"""
---------------------------------task---------------------------------
"""
def add_task(request, sid):
    if request.method == "POST":
        form = TaskForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
        return HttpResponseRedirect(reverse("spider_detail", kwargs={"sid":sid}))

    elif request.method == "GET":
        spider = Spider.objects.get(id=sid)
        task_id = int(round(time.time() * 1000))
        empty_form = TaskForm(initial={"spider":spider, "task_id":task_id})
        return render(request, "spider/task_add.html", {"form":empty_form})


def update_task(request, tid):
    task = Task.objects.get(task_id=tid)
    spider = task.spider
    sid = spider.id
    if request.method == "POST":
        form = TaskForm(request.POST)
        if form.is_valid():
            form_data = form.cleaned_data
            for field in form_data.keys():
                setattr(task, field, form_data[field])
            task.save()
            return HttpResponseRedirect(reverse("spider_detail", kwargs={"sid":sid}))
            #return JsonResponse({"status":"ok!"})
    else:
        empty_form = TaskForm(initial=model_to_dict(task))
        return render(request, "spider/task_add.html", {"form":empty_form})


def del_task(request):
    try:
        tid = int(request.GET["tid"])
        sid = int(request.GET['sid'])
    except:
        return HttpResponseRedirect(reverse("spiders"))
    else:
        Task.objects.filter(task_id=tid).delete()
        return HttpResponseRedirect(reverse("spider_detail", kwargs={"sid":sid}))


def test(request):
    spider = Spider.objects.get(id=5)
    keys = Spider.get_sorted_keys()
    values = [getattr(spider, k) for k in keys]
    spider_id = spider.id
    return render(request, "spider/test.html", {"kv": zip(keys, values), "spider_id":spider_id})


def list_joblog(request):
    ip = "192.168.2.12"
    port = "6800"
    project = "news"
    spider = "evzhidao"
    scpdcli = ScrapydClient.get_cli(ip, port)
    all_jobs = scpdcli.list_jobs(project)
    jobs = all_jobs.get(spider, [])
    logs = []
    for job in jobs:
        url = scpdcli.get_logurl(project, spider, job[0])
        logs.append({"log_url": url, "log_starttime":job[1]})
    return JsonResponse({"logs":logs})
